
#include <stdlib.h>
#include "SecurityTokenPassword.h"

/** Constructor for the password-based security token.
 * 
 * @param ipassword The password to use. */
SecurityTokenPassword::SecurityTokenPassword(const char *ipassword)
{
   tried = 0;
   password = (ipassword) ? strdup(ipassword) : NULL;
}

/** Destructor for the password-based security token. */
SecurityTokenPassword::~SecurityTokenPassword()
{
   if (password) free(password);
   password = NULL;
}

/** SSH session authentication call.
 * 
 * @param tsession The SSH session to authenticate.
 * 
 * @param username The username to use with the password for SSH
 * authentication.
 * 
 * @return Zero on success, negative on failure. */
int SecurityTokenPassword::authenticateSSHSession(LIBSSH2_SESSION *tsession, const char *username)
{
   void **abstract_pointer = libssh2_session_abstract(tsession);
   *abstract_pointer = (void *)this;
   return libssh2_userauth_password(tsession,
      (username) ? (username) : "",
      (password) ? (password) : "");
}
